package com.xbongbong.pro.message.service.impl;/*/*/

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.helper.AuthHelper;
import com.alibaba.dingtalk.openapi.helper.Env;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationProgressMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendByTemplateDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendResultMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.SendToConversationMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.entity.AbstractMessage;
import com.alibaba.dingtalk.openapi.newmessage.entity.Msg;
import com.alibaba.dingtalk.openapi.newmessage.entity.template.TemplateMessage;
import com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper;
import com.alibaba.dingtalk.openapi.newmessage.helper.SendToConversationHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.MessageErrorCodeEnum;
import com.xbongbong.pro.message.service.MessageNewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/22 15:37
 * @since v1.0
 * @version v1.0
 */
@Service("messageNewService")
public class MessageNewServiceImpl implements MessageNewService {

    private static final Logger LOG = LoggerFactory
            .getLogger(MessageNewServiceImpl.class);

    @Resource
    private AuthHelper authHelper;

    @Override
    public CorpConversationHelper.SendResponse dingtalkOAMessagePushToUserAndDept(String corpid, AbstractMessage message, String userIdList, String depIdList) throws XbbException {
        String accessToken;
        String agentId;
        try {
            accessToken = authHelper.getAccessToken(corpid);
            agentId = authHelper.getAgentId(corpid, Env.DING_XBB_APP_ID);
        } catch (Exception e) {
            LOG.error("获取accessToken或agentId失败", e);
            throw new XbbException(MessageErrorCodeEnum.API_ERROR_380002);
        }

        CorpConversationSendMsgDelivery corpConversationSendMsgDelivery = new CorpConversationSendMsgDelivery();
        if (!Objects.isNull(userIdList) && !Objects.equals(userIdList, "") && userIdList.length() > 0) {
            corpConversationSendMsgDelivery.setUseridList(userIdList);
        }
        if (!Objects.isNull(depIdList) && !Objects.equals(depIdList, "") && depIdList.length() > 0) {
            corpConversationSendMsgDelivery.setDeptIdList(depIdList);
        }
        corpConversationSendMsgDelivery.setAgentId(Long.valueOf(agentId));
        Msg msg = new Msg(message);
        corpConversationSendMsgDelivery.setMsg(msg);
        CorpConversationHelper.SendResponse response = null;
        try {
            response = CorpConversationHelper.send(accessToken, corpConversationSendMsgDelivery);
        } catch (OApiException e) {
            LOG.error("推送失败,钉钉接口错误", e);
        } catch (Exception e) {
            LOG.error("CorpConversationHelper.send() 错误，非钉钉接口错误，可能是socket错误:",e);
        }
        return response;
    }

    @Override
    public CorpConversationHelper.ProgressResponse dingtalkOAMessagePushProgress(String corpid, Long taskId) throws XbbException {
        String accessToken;
        String agentId;
        try {
            accessToken = authHelper.getAccessToken(corpid);
            agentId = authHelper.getAgentId(corpid, Env.DING_XBB_APP_ID);
        } catch (Exception e) {
            LOG.error("获取accessToken或agentId失败", e);
            throw new XbbException(MessageErrorCodeEnum.API_ERROR_380002);
        }

        CorpConversationProgressMsgDelivery corpConversationProgressMsgDelivery = new CorpConversationProgressMsgDelivery();
        corpConversationProgressMsgDelivery.setAgentId(Long.valueOf(agentId));
        corpConversationProgressMsgDelivery.setTaskId(taskId);
        CorpConversationHelper.ProgressResponse response = null;
        try {
            response = CorpConversationHelper.progress(accessToken, corpConversationProgressMsgDelivery);
        } catch (OApiException e) {
            LOG.error("查询消息进度失败,钉钉接口错误，taskId=" + taskId + " ,corpid=" + corpid, e);
        } catch (Exception e) {
            LOG.error("CorpConversationHelper.progress() 错误，非钉钉接口错误，可能是socket错误:",e);
        }
        return response;
    }

    @Override
    public CorpConversationHelper.SendResultResponse dingtalkOAMessagePushSendResult(String corpid, Long taskId) throws XbbException {
        String accessToken;
        String agentId;
        try {
            accessToken = authHelper.getAccessToken(corpid);
            agentId = authHelper.getAgentId(corpid, Env.DING_XBB_APP_ID);
        } catch (Exception e) {
            LOG.error("获取accessToken或agentId失败", e);
            throw new XbbException(MessageErrorCodeEnum.API_ERROR_380002);
        }

        CorpConversationSendResultMsgDelivery corpConversationSendResultMsgDelivery = new CorpConversationSendResultMsgDelivery();
        corpConversationSendResultMsgDelivery.setAgentId(Long.valueOf(agentId));
        corpConversationSendResultMsgDelivery.setTaskId(taskId);
        CorpConversationHelper.SendResultResponse response = null;
        try {
            response = CorpConversationHelper.sendResult(accessToken, corpConversationSendResultMsgDelivery);
        } catch (OApiException e) {
            LOG.error("查询工作通知消息的发送结果失败,钉钉接口错误，taskId=" + taskId + " ,corpid=" + corpid, e);
        } catch (Exception e) {
            LOG.error("CorpConversationHelper.sendResult() 错误，非钉钉接口错误，可能是socket错误:",e);
        }
        return response;
    }

    @Override
    public SendToConversationHelper.SendToConversationResponse dingtalkSendToConversation(String corpid, String userIdList, String cid, Msg msg) throws XbbException {
        String accessToken;
        try {
            accessToken = authHelper.getAccessToken(corpid);
        } catch (Exception e) {
            LOG.error("获取accessToken失败", e);
            throw new XbbException(MessageErrorCodeEnum.API_ERROR_380002);
        }
        SendToConversationMsgDelivery sendToConversationMsgDelivery = new SendToConversationMsgDelivery();
        sendToConversationMsgDelivery.setSender(userIdList);
        sendToConversationMsgDelivery.setCid(cid);
        sendToConversationMsgDelivery.setMsg(msg);
        SendToConversationHelper.SendToConversationResponse response = null;
        try {
            response = SendToConversationHelper.send(accessToken, sendToConversationMsgDelivery);
        } catch (OApiException e) {
            LOG.error("发送普通消息失败,钉钉接口错误，cid=" + cid + " ,corpid=" + corpid, e);
        } catch (Exception e) {
            LOG.error("SendToConversationHelper.send() 错误，非钉钉接口错误，可能是socket错误:",e);
        }

        return response;
    }

    @Override
    public CorpConversationHelper.SendByTemplateResponse dingtalkWorkMessageByTemplatePushToUserAndDept(String corpid, TemplateMessage templateMessage, String userIdList, String depIdList) throws XbbException {
        String accessToken;
        String agentId;
        try {
            accessToken = authHelper.getAccessToken(corpid);
            agentId = authHelper.getAgentId(corpid, Env.DING_XBB_APP_ID);
        } catch (Exception e) {
            LOG.error("获取accessToken或agentId失败", e);
            throw new XbbException(MessageErrorCodeEnum.API_ERROR_380002);
        }

        CorpConversationSendByTemplateDelivery corpConversationSendByTemplateDelivery = new CorpConversationSendByTemplateDelivery();
        if (!Objects.isNull(userIdList) && !Objects.equals(userIdList, "") && userIdList.length() > 0) {
            corpConversationSendByTemplateDelivery.setUseridList(userIdList);
        }
        if (!Objects.isNull(depIdList) && !Objects.equals(depIdList, "") && depIdList.length() > 0) {
            corpConversationSendByTemplateDelivery.setDeptIdList(depIdList);
        }

        corpConversationSendByTemplateDelivery.setTemplateId(templateMessage.getTemplateId());

        corpConversationSendByTemplateDelivery.setAgentId(Long.valueOf(agentId));

        corpConversationSendByTemplateDelivery.setData(templateMessage.getFormMessage().toJsonObject().toJSONString());
        CorpConversationHelper.SendByTemplateResponse response = null;
        try {
            response = CorpConversationHelper.sendByTemplate(accessToken, corpConversationSendByTemplateDelivery);
        } catch (OApiException e) {
            LOG.error("com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.sendByTemplate 推送失败,钉钉接口错误", e);
        } catch (Exception e) {
            LOG.error("com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.sendByTemplate 错误，非钉钉接口错误，可能是socket错误:",e);
        }
        return response;
    }
}
